package org.bluedream.core.module.sys.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.bluedream.comm.base.BaseController;
import org.bluedream.core.utils.UserUtil;
import org.bluedream.core.utils.configUtil.GoalConfigUtil;
import org.bluedream.core.config.advice.RsaSecurityParameter;
import org.bluedream.core.module.sys.entity.User;
import org.bluedream.core.module.sys.service.MenuService;
import org.bluedream.core.module.sys.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Slf4j
@Controller
@RequestMapping(value = "${adminPath}/user")
public class UserController extends BaseController {
    @Resource
    private UserService userService;

    @Resource
    private MenuService menuService;

    @ModelAttribute
    public User get(String id){
        return userService.getById(id);
    }

    @RequiresPermissions("user:view")
    @RequestMapping(value = {"/list" , ""})
    public String list(User user, Model model){
        model.addAttribute("pageSize" , GoalConfigUtil.getValueByKey("sys.page.size"));
        model.addAttribute("user" , user);
        return "sys/user/userList";
    }

    @RequiresPermissions("user:view")
    @RequestMapping("/form")
    public String form(User user , Model model){
        model.addAttribute("user" , user);
        return "sys/user/userForm";
    }

    @RequiresPermissions("user:view")
    @RequestMapping("/findPage")
    @ResponseBody
    public IPage<User> findPage(@RsaSecurityParameter User user){
        return userService.findPage(user);
    }

/*    @RequiresPermissions("user:view")
    @RequestMapping("/findPageForRole")
    @ResponseBody
    public IPage<User> findPageForRole(@RsaSecurityParameter User user , @RsaSecurityParameter String roleId){
        return userService.findPageForRole(user , roleId);
    }*/

    @RequiresPermissions("user:edit")
    @RequestMapping("/save")
    @ResponseBody
    public String save(User user){
        boolean flag = userService.save(user);
        if (flag){
            return "用户信息保存成功";
        }
        return "用户信息保存失败";
    }

    @RequiresPermissions("user:password")
    @RequestMapping("/modifyPasswordForm")
    public String modifyPasswordForm(Model model){
        model.addAttribute("user" , UserUtil.getLoginInfo());
        return "sys/user/modifyPasswordForm";
    }

    @RequiresPermissions("user:password")
    @RequestMapping("/modifyPassword")
    @ResponseBody
    public String modifyPassword(@RsaSecurityParameter String pwd){
        boolean flag = userService.modifyPassword(pwd);
        if (flag){
            return "密码修改成功";
        }
        return "密码修改失败";
    }

    @RequiresPermissions("user:edit")
    @RequestMapping("/resetPassword")
    @ResponseBody
    public String  resetPassword(@RsaSecurityParameter User user){
        boolean flag = userService.resetPassword(user);
        if (flag){
            return "重置密码成功，新密码是：" + GoalConfigUtil.getValueByKey("sys.user.initPassword");
        }
        return "重置密码失败，请联系系统管理员";
    }

    @RequiresPermissions("user:view")
    @RequestMapping("/treeData")
    @ResponseBody
    public List<Map<String , Object>> treeData(@RequestParam(defaultValue="1")String isShowCode , @RequestParam(defaultValue="undefined")String excludeCode){
        return userService.treeData(excludeCode , isShowCode);
    }

    @RequiresPermissions("user:password")
    @RequestMapping("/checkPassword")
    @ResponseBody
    public boolean checkPassword(@RsaSecurityParameter String pwd){
        return userService.checkPassword(pwd);
    }
}
